#
# Makefile
# lizongzhe, 2016-06-21 11:45
#
IMAGE_NAME=esr_parser_env
PORT?=8080
ENV_FILE?=deploy/local.env
TEST_ENV_FILE?=deploy/test.env

init:
	docker build -t $(IMAGE_NAME) image

bash:
	@docker run -it --rm -v `pwd`:/work -w /work/src --env-file $(ENV_FILE) $(IMAGE_NAME) /bin/bash

worker: name=worker
worker: 
	@docker run -d  --log-driver=none -v `pwd`:/work -w /work/src --env-file $(ENV_FILE) --name ${name} $(IMAGE_NAME) celery -A conf worker -c 1
	@docker inspect -f '{{ .State.Pid }}' ${name} > /run/${name}.pid

runserver: name=dashboard
runserver:
	@docker run -itd  --log-driver=none -v `pwd`:/work -w /work/src -p $(PORT):$(PORT) --env-file $(ENV_FILE) --name ${name} $(IMAGE_NAME) python manage.py runserver 0:$(PORT)
	@docker inspect -f '{{ .State.Pid }}' ${name} > /run/${name}.pid

test:
	@docker run -it --rm -v `pwd`:/work -w /work/src -p $(PORT):$(PORT) --env-file $(TEST_ENV_FILE)  $(IMAGE_NAME) python manage.py test


ESR_IMAGE:
	@echo FROM $(IMAGE_NAME) 
	@echo ADD . /work 
	@echo WORKDIR /work
	@cat $(ENV_FILE) | awk '{print "env "$$0}'

load_jobs:
	@echo load_jobs >> /tmp/log
	@docker run --rm -v `pwd`:/work -w /work/src --env-file $(ENV_FILE)  $(IMAGE_NAME) python manage.py load_jobs
	@echo load_jobs_end >> /tmp/log

process_jobs:
	@echo process_jobs >> /tmp/log
	@docker run --rm -v `pwd`:/work -w /work/src --env-file $(ENV_FILE)  $(IMAGE_NAME) python manage.py process_jobs
	@echo process_end >> /tmp/log

log:
	@date >> /tmp/log


dashboard:
	@cp deploy/dashboard /etc/monit/conf.d/dashboard && /etc/init.d/monit restart


deploy_worker:
ifdef IP
	@fab -f fab.py -H $(IP) -u root -p Gliacloud2016 init
else
	@fab -f fab.py -u root -p Gliacloud2016 init
endif


update_worker:
ifdef IP
	@fab -f fab.py -H $(IP) -u root -p Gliacloud2016 update_node
else
	@fab -f fab.py -u root -p Gliacloud2016 update_node
endif
